class Solution {
    public int search(int[] nums, int target) {
        if(0 == nums.length) {
            return 0;
        }
        int sum = 0;
        int left = 0;
        int right = nums.length-1; 
        //先找右边界
        while(left<=right) {
            int mid = (left+right)/2;
            if(target >= nums[mid]) {
                left = mid+1;
            }else {
                right = mid-1;
            }
        }
        int rightIndex = left;
        if(right>=0 && nums[right] != target) {
            return 0;
        }
        left = 0;
        //找左边界
        while(left<=right) {
            int mid = (left+right)/2;
            if(target <= nums[mid]) {
                right = mid-1;
            }else {
                left = mid+1;
            }
        }
        int leftIndex = right;
        return rightIndex - leftIndex-1;
    }
}